<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>viewportsizemonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
  </script>

  <script src="static/js/doc.js">
  </script>

  <meta charset="utf8">
</head>

<body onload="prettyPrint()">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_viewportsizemonitor.js.html">viewportsizemonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line2"></a>// you may not use this file except in compliance with the License.
<a name="line3"></a>// You may obtain a copy of the License at
<a name="line4"></a>//
<a name="line5"></a>//     http://www.apache.org/licenses/LICENSE-2.0
<a name="line6"></a>//
<a name="line7"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line8"></a>// distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
<a name="line9"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line10"></a>// See the License for the specific language governing permissions and
<a name="line11"></a>// limitations under the License.
<a name="line12"></a>
<a name="line13"></a>// Copyright 2007 Google Inc. All Rights Reserved.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Utility class that monitors viewport size changes.
<a name="line17"></a> *
<a name="line18"></a> * @see ../demos/viewportsizemonitor.html
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.dom.ViewportSizeMonitor&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.dom&#39;);
<a name="line24"></a>goog.require(&#39;goog.events&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line27"></a>goog.require(&#39;goog.math.Size&#39;);
<a name="line28"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * This class can be used to monitor changes in the viewport size.  Instances
<a name="line33"></a> * dispatch a {@link goog.events.EventType.RESIZE} event when the viewport size
<a name="line34"></a> * changes.  Handlers can call {@link goog.dom.ViewportSizeMonitor#getSize} to
<a name="line35"></a> * get the new viewport size.
<a name="line36"></a> *
<a name="line37"></a> * Use this class if you want to execute resize/reflow logic each time the
<a name="line38"></a> * user resizes the browser window.  This class is guaranteed to only dispatch
<a name="line39"></a> * {@code RESIZE} events when the pixel dimensions of the viewport change.
<a name="line40"></a> * (Internet Explorer fires resize events if any element on the page is resized,
<a name="line41"></a> * even if the viewport dimensions are unchanged, which can lead to infinite
<a name="line42"></a> * resize loops.)
<a name="line43"></a> *
<a name="line44"></a> * Example usage:
<a name="line45"></a> *  &lt;pre&gt;
<a name="line46"></a> *    var vsm = new goog.dom.ViewportSizeMonitor();
<a name="line47"></a> *    goog.events.listen(vsm, goog.events.EventType.RESIZE, function(e) {
<a name="line48"></a> *      alert(&#39;Viewport size changed to &#39; + vsm.getSize());
<a name="line49"></a> *    });
<a name="line50"></a> *  &lt;/pre&gt;
<a name="line51"></a> *
<a name="line52"></a> * Manually verified on IE6, IE7, FF2, Opera 9, and WebKit.  {@code getSize}
<a name="line53"></a> * doesn&#39;t always return the correct viewport height on Safari 2.0.4.
<a name="line54"></a> *
<a name="line55"></a> * @param {Window} opt_window The window to monitor; defaults to the window in
<a name="line56"></a> *    which this code is executing.
<a name="line57"></a> * @constructor
<a name="line58"></a> * @extends {goog.events.EventTarget}
<a name="line59"></a> */
<a name="line60"></a>goog.dom.ViewportSizeMonitor = function(opt_window) {
<a name="line61"></a>  goog.events.EventTarget.call(this);
<a name="line62"></a>
<a name="line63"></a>  // Default the window to the current window if unspecified.
<a name="line64"></a>  this.window_ = opt_window || window;
<a name="line65"></a>
<a name="line66"></a>  // Listen for window resize events.
<a name="line67"></a>  this.listenerKey_ = goog.events.listen(this.window_,
<a name="line68"></a>      goog.events.EventType.RESIZE, this.handleResize_, false, this);
<a name="line69"></a>
<a name="line70"></a>  // Set the initial size.
<a name="line71"></a>  this.size_ = goog.dom.getViewportSize(this.window_);
<a name="line72"></a>
<a name="line73"></a>  if (this.isPollingRequired_()) {
<a name="line74"></a>    this.windowSizePollInterval_ = window.setInterval(
<a name="line75"></a>        goog.bind(this.checkForSizeChange_, this),
<a name="line76"></a>        goog.dom.ViewportSizeMonitor.WINDOW_SIZE_POLL_RATE);
<a name="line77"></a>  }
<a name="line78"></a>};
<a name="line79"></a>goog.inherits(goog.dom.ViewportSizeMonitor, goog.events.EventTarget);
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * Rate in milliseconds at which to poll the window size on browsers that
<a name="line84"></a> * need polling.
<a name="line85"></a> * @type {number}
<a name="line86"></a> */
<a name="line87"></a>goog.dom.ViewportSizeMonitor.WINDOW_SIZE_POLL_RATE = 500;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * Event listener key for window the window resize handler, as returned by
<a name="line92"></a> * {@link goog.events.listen}.
<a name="line93"></a> * @type {?number}
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.dom.ViewportSizeMonitor.prototype.listenerKey_ = null;
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * The window to monitor.  Defaults to the window in which the code is running.
<a name="line101"></a> * @type {Window?}
<a name="line102"></a> * @private
<a name="line103"></a> */
<a name="line104"></a>goog.dom.ViewportSizeMonitor.prototype.window_ = null;
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * The most recently recorded size of the viewport, in pixels.
<a name="line109"></a> * @type {goog.math.Size?}
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.dom.ViewportSizeMonitor.prototype.size_ = null;
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Identifier for the interval used for polling the window size on Windows
<a name="line117"></a> * Safari.
<a name="line118"></a> * @type {number?}
<a name="line119"></a> * @private
<a name="line120"></a> */
<a name="line121"></a>goog.dom.ViewportSizeMonitor.prototype.windowSizePollInterval_ = null;
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Checks if polling is required for this user agent. Opera only requires
<a name="line126"></a> * polling when the page is loaded within an IRAME.
<a name="line127"></a> * @return {boolean} Whether polling is required.
<a name="line128"></a> * @private
<a name="line129"></a> */
<a name="line130"></a>goog.dom.ViewportSizeMonitor.prototype.isPollingRequired_ = function() {
<a name="line131"></a>  return goog.userAgent.WEBKIT &amp;&amp; goog.userAgent.WINDOWS ||
<a name="line132"></a>      goog.userAgent.OPERA &amp;&amp; this.window_.self != this.window_.top;
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Returns the most recently recorded size of the viewport, in pixels.  May
<a name="line138"></a> * return null if no window resize event has been handled yet.
<a name="line139"></a> * @return {goog.math.Size} The viewport dimensions, in pixels.
<a name="line140"></a> */
<a name="line141"></a>goog.dom.ViewportSizeMonitor.prototype.getSize = function() {
<a name="line142"></a>  // Return a clone instead of the original to preserve encapsulation.
<a name="line143"></a>  return this.size_ ? this.size_.clone() : null;
<a name="line144"></a>};
<a name="line145"></a>
<a name="line146"></a>
<a name="line147"></a>/**
<a name="line148"></a> * Disposes of the object.
<a name="line149"></a> */
<a name="line150"></a>goog.dom.ViewportSizeMonitor.prototype.disposeInternal = function() {
<a name="line151"></a>  goog.dom.ViewportSizeMonitor.superClass_.disposeInternal.call(this);
<a name="line152"></a>
<a name="line153"></a>  if (this.listenerKey_) {
<a name="line154"></a>    goog.events.unlistenByKey(this.listenerKey_);
<a name="line155"></a>    this.listenerKey_ = null;
<a name="line156"></a>  }
<a name="line157"></a>
<a name="line158"></a>  if (this.windowSizePollInterval_) {
<a name="line159"></a>    window.clearInterval(this.windowSizePollInterval_);
<a name="line160"></a>    this.windowSizePollInterval_ = null;
<a name="line161"></a>  }
<a name="line162"></a>
<a name="line163"></a>  this.window_ = null;
<a name="line164"></a>  this.size_ = null;
<a name="line165"></a>};
<a name="line166"></a>
<a name="line167"></a>
<a name="line168"></a>/**
<a name="line169"></a> * Handles window resize events by measuring the dimensions of the
<a name="line170"></a> * viewport and dispatching a {@link goog.events.EventType.RESIZE} event if the
<a name="line171"></a> * current dimensions are different from the previous ones.
<a name="line172"></a> * @param {goog.events.Event} event The window resize event to handle.
<a name="line173"></a> * @private
<a name="line174"></a> */
<a name="line175"></a>goog.dom.ViewportSizeMonitor.prototype.handleResize_ = function(event) {
<a name="line176"></a>  this.checkForSizeChange_();
<a name="line177"></a>};
<a name="line178"></a>
<a name="line179"></a>
<a name="line180"></a>/**
<a name="line181"></a> * Measures the dimensions of the viewport and dispatches a
<a name="line182"></a> * {@link goog.events.EventType.RESIZE} event if the current dimensions are
<a name="line183"></a> * different from the previous ones.
<a name="line184"></a> * @private
<a name="line185"></a> */
<a name="line186"></a>goog.dom.ViewportSizeMonitor.prototype.checkForSizeChange_ = function() {
<a name="line187"></a>  var size = goog.dom.getViewportSize(this.window_);
<a name="line188"></a>  if (!goog.math.Size.equals(size, this.size_)) {
<a name="line189"></a>    this.size_ = size;
<a name="line190"></a>    this.dispatchEvent(goog.events.EventType.RESIZE);
<a name="line191"></a>  }
<a name="line192"></a>};
</pre>


</body>
</html>
